#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<vector>

class Solution {
public:
    int jewelleryValue(vector<vector<int>>& frame)
    {
        int row = frame.size();
        int col = frame[0].size();

        vector<vector<int>> dp(row + 1, vector<int>(col + 1));

        for (int i = 1; i <= row; i++)
        {
            for (int j = 1; j <= col; j++)
            {
                dp[i][j] = frame[i - 1][j - 1] + max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
        return dp[row][col];
    }
};

class Solution {
public:
    int minFallingPathSum(vector<vector<int>>& matrix)
    {
        int row = matrix.size();
        int col = matrix[0].size();

        vector<vector<int>> dp(row, vector<int>(col + 2, INT_MAX));

        for (int i = 1; i < col + 1; i++)
        {
            dp[0][i] = matrix[0][i - 1];
        }

        for (int i = 1; i < row; i++)
        {
            for (int j = 1; j < col + 1; j++)
            {
                dp[i][j] = matrix[i][j - 1] +
                    min(min(dp[i - 1][j - 1], dp[i - 1][j]), min(dp[i - 1][j], dp[i - 1][j + 1]));
            }
        }

        int ret = INT_MAX;
        for (int i = 0; i < col + 2; i++)
        {
            ret = min(ret, dp[row - 1][i]);
        }
        return ret;
    }
};